<div class="row spacing-bottom 2col">   
    <div class="col-md-3 col-sm-6 spacing-bottom-sm">   
        <div class="tiles green added-margin">
          <div class="tiles-body">
                <div class="tiles-title">
                    商品总数
                </div>  
                <div class="heading">
                    <span><?= $total ?></span>个
                </div>             
                <div class="description">
                    <h4 class="mini-description ">
                        <i class="icon-plus-sign"></i>&nbsp;
                        <a class="white" href="javascript:void(0)" onclick="$.Goods.add()">新增一个商品</a>
                    </h4>
                </div>
            </div>  
        </div>
    </div>              
</div>

<div class="row">
    <div class="col-md-12">
      <div class="grid simple">
        <div class="grid-title no-border">
            <h4>商品 <span class="semi-bold">查询</span></h4>
            <div class="tools"> 
                <a class="collapse" href="javascript:;"></a>
            </div>
        </div>
        <div class="grid-body no-border">
            <form class="form-no-horizontal-spacing" method="GET" action="<?= DOMAIN.'goods/browse' ?>">
                <div class="row column-seperation">
                    <div class="col-md-4">
                        <div class="form-group col-md-10">
                            <label class="form-label">编码</label>
                            <input type="text" class="form-control" name="goods_sn" value="<?= $default['goods_sn'] ?>" placeholder="eg.G0001">
                        </div>

                        <div class="form-group col-md-10">
                            <label class="form-label">名称</label>
                            <input type="text" class="form-control" name="name" value="<?= $default['name'] ?>">
                        </div>
                        <div class="form-group col-md-10">
                            <label class="form-label">英文名称</label>
                            <input type="text" class="form-control" name="subname" value="<?= $default['subname'] ?>">
                        </div>
                        <br>
                    </div>

                    <div class="col-md-4">
                        <div class="form-group col-md-10">
                            <label class="form-label">状态</label>
                            <div class="radio radio-control">
                                <input type="radio" checked="checked" id="status-ALL" name="status" value="">
                                <label for="status-ALL">不限</label>
                                <? foreach($goods_status as $k => $v): ?>
                                    <? if($default['status'] == $k): ?>
                                        <input type="radio" checked="checked" value="<?= $k ?>" name="status" id="<?= "status-{$k}" ?>">
                                    <? else: ?>
                                        <input type="radio" value="<?= $k ?>" name="status" id="<?= "status-{$k}" ?>">
                                    <? endif ?>
                                    <label for="<?= "status-{$k}" ?>"><?= $v ?></label>
                                <? endforeach ?>
                            </div>
                        </div>

                        <div class="form-group col-md-10">
                            <label class="form-label">品牌</label>
                            <select name="brand_sn" class="select2 form-control">
                                <option value="0" selected="selected">不限</option>
                                <? foreach($brand_sn as $v): ?>
                                    <option value="<?= $v['brand_sn'] ?>" <?= ($default['brand_sn'] == $v['brand_sn'] ) ? 'selected="selected"' : "" ?>>
                                        <?= $v['name'] ?>
                                    </option>
                                <? endforeach ?>
                            </select>
                        </div>
                        <br>
                        <div class="form-group col-md-10">
                            <label class="form-label">分类</label>
                            <select name="category_id" class="select2 form-control">
                                <option value="0" selected="selected">不限</option>
                                <? foreach($category as $cg): ?>
                                    <option value="<?= $cg['category_id'] ?>" <?= ($default['category_id'] == $cg['category_id'] ) ? 'selected="selected"' : "" ?>>
                                        <?= $cg['name'] ?>
                                    </option>
                                    <? if($cg['children']): ?>
                                        <? foreach($cg['children'] as $c): ?>
                                            <option value="<?= $c['category_id'] ?>" <?= ($default['category_id'] == $c['category_id'] ) ? 'selected="selected"' : "" ?>>
                                              &nbsp;&nbsp;&nbsp;&nbsp;
                                              <?= $c['name'] ?>
                                            </option>
                                        <? endforeach ?>
                                    <? endif ?>
                                <? endforeach ?>
                            </select>
                        </div>
                        <div class="form-group col-md-10">
                            <label class="form-label">属性</label>
                            <select name="attr_group_id" class="select2 form-control" onchange="$.Goods.get_attr('attr', this.value)">
                                <option value="0" selected="selected">不限</option>
                                <? if($attr_group): ?>
                                    <? foreach($attr_group as $ag): ?>
                                        <option value="<?= $ag['attr_group_id'] ?>" <?= ($default['attr_group_id'] == $ag['attr_group_id'] ) ? 'selected="selected"' : "" ?>>
                                            <?= $ag['name'] ?>
                                        </option>
                                    <? endforeach ?>
                                <? endif ?>
                            </select>

                            <select name="attr_id" class="select2 form-control" onchange="$.Goods.get_attr('attr_value', this.value)">
                                <option value="0" selected="selected">不限</option>
                                <? if($attr): ?>
                                    <? foreach($attr as $a): ?>
                                        <option value="<?= $a['attr_id'] ?>" <?= ($default['attr_id'] == $a['attr_id'] ) ? 'selected="selected"' : "" ?>>
                                            <?= $a['name'] ?>
                                        </option>
                                    <? endforeach ?>
                                <? endif ?>
                            </select>
                            
                            <select name="attr_value_id" class="select2 form-control" >
                                <option value="0" selected="selected">不限</option>
                                <? if($attr_value): ?>
                                    <? foreach($attr_value as $av): ?>
                                        <option value="<?= $av['attr_value_id'] ?>" <?= ($default['attr_value_id'] == $av['attr_value_id'] ) ? 'selected="selected"' : "" ?>>
                                            <?= $av['name'] ?>
                                        </option>
                                    <? endforeach ?>
                                <? endif ?>
                            </select>
                        </div>
                        <br>
                    </div>

                    <div class="col-md-4">
                        <div class="form-group col-md-12">
                            <label class="form-label">创建时间</label><br>
                            <input type="text" name="create_time_start" value="<?= $default['create_time_start'] ?>" class="datepicker form-inline col-md-5">
                            <div class="form-inline col-md-2"> ~ </div>
                            <input type="text" name="create_time_end" value="<?= $default['create_time_end'] ?>"class="datepicker form-inline col-md-5">
                        </div><br><br>
                        <div class="form-group col-md-12">
                            <label class="form-label">更新时间</label><br>
                            <input type="text" name="update_time_start" value="<?= $default['update_time_start'] ?>"class="datepicker form-inline col-md-5">
                            <div class="form-inline col-md-2"> ~ </div>
                            <input type="text" name="update_time_end" value="<?= $default['update_time_end'] ?>"class="datepicker form-inline col-md-5">
                        </div>
                    </div>
                </div>

                <div class="form-actions">
                    <div class="pull-left">
                      <button type="submit" class="btn btn-warning btn-cons"><i class="icon-search"></i>&nbsp;查询</button>
                      <button type="button" class="btn btn-white btn-cons" onclick="$.T.reset_form(this)">重置</button>
                    </div>
                </div>
            </form>
        </div>
      </div>
    </div>
</div>

<? if($list): ?>
    <div class="row-fluid">
        <div class="grid simple">
            <div class="grid-title no-border">
            </div>
            <div class="grid-body no-border">
                <? if($list['data']): ?>
                <table id="data-table" class="table table-striped">
                    <colgroup>
                        <col width="10px">
                        <col width="20px">
                        <col width="">
                        <col width="">
                        <col width="">
                        <col width="">
                        <col width="">
                        <col width="">
                        <col width="60px">
                        <col width="120px">
                        <col width="120px">
                        <col width="150px">
                    </colgroup>
                    <thead>
                        <tr>
                            <th>
                                <div class="checkbox check-success">
                                    <input type="checkbox" value="" id="select-all" onclick="$.T.select(this, 'goods-sn')">
                                    <label for="select-all"></label>
                                </div>
                            </th>
                            <th>状态</th>
                            <th>图片</th>
                            <th>编码</th>
                            <th>分类</th>
                            <th>品牌</th>
                            <th>名称</th>
                            <th>英文名称</th>
                            <th>价格</th>
                            <th>创建时间</th>
                            <th>更新时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <? foreach($list['data'] as $item): ?>
                        <tr>
                            <td>
                                <div class="checkbox check-success">
                                    <input type="checkbox" value="<?= $item['goods_sn'] ?>" class="goods-sn" name="goods_sn[]" id="<?= $item['goods_sn'] ?>">
                                    <label for="<?= $item['goods_sn'] ?>"></label>
                                </div>
                            </td>
                            <td>
                                <span class="label <?= $item['status'] == GOODS_STATUS_OFF ? 'label-important' : '' ?>">
                                    <?= $goods_status[$item['status']] ?>
                                </span>
                            </td>
                             <td>
                                <?= goods_img($item['goods_sn'], 1, 'small') ?>
                            </td>
                            <td><?= $item['goods_sn'] ?></td>
                            <td><?= $item['category_info']['name'] ?></td>
                            <td><?= $item['brand_info']['name'] ?></td>
                            <td><?= $item['name'] ?></td>
                            <td>
                                <span class="muted"><?= $item['subname'] ?></span>
                            </td>
                            <td>
                                <?= price_format($item['base_price']) ?>
                            </td>
                           
                            <td>
                                <span class="muted"><?= time_format($item['create_time']) ?></span>
                            </td>
                            <td>
                                <span class="muted"><?= time_format($item['update_time']) ?></span>
                            </td>
                            <td>
                                <? if($item['status'] == GOODS_STATUS_OFF): ?>
                                    <a href="javascript:void(0)" onclick="$.Goods.change_status('<?= $item['goods_sn']?>', 'on');" >
                                        <i class="icon-rocket"></i>
                                        启用
                                    </a>
                                <? else: ?>
                                    <a href="javascript:void(0)" onclick="$.Goods.change_status('<?= $item['goods_sn']?>', 'off');" >
                                        <i class="icon-ban-circle"></i>
                                        停用
                                    </a>
                                    &nbsp;
                                    <a href="javascript:void(0)" onclick="$.Goods.edit('<?= $item['goods_sn'] ?>')">
                                        <i class="icon-paste"></i>
                                        编辑
                                    </a>
                                <? endif ?>
                            </td>
                        </tr>
                        <? endforeach ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td></td>
                            <td colspan="12">
                                <div class="btn-group"> 
                                    <a href="#" data-toggle="dropdown" class="btn btn-danger dropdown-toggle btn-demo-space"> <i class="icon-copy"></i> 批量操作 <span class="caret"></span> 
                                    </a>
                                    <ul class="dropdown-menu">
                                        <li>
                                            <a href="javascript:void(0)" onclick="$.Goods.batch_act('batch_change_status')">
                                                <span class="muted">批量编辑</span> 状态
                                            </a>
                                        </li>
                                        <li>
                                            <a href="javascript:void(0)" onclick="$.Goods.batch_act('batch_edit_brand')">
                                                <span class="muted">批量编辑</span> 品牌
                                            </a>
                                        </li>
                                        
                                    </ul>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="12">
                                每页
                                <select class="select2 pagesize">
                                    <? foreach($pagesize as $k => $v): ?>
                                        <option value="<?= $k ?>" <?= $k == $list['size'] ? 'selected="selected"' : '' ?>><?= $v ?></option>
                                    <? endforeach ?>
                                </select>
                                <?= $pagination ?>
                                <div class="pagination_info">
                                    总计：<?= $list['total'] ?>
                                </div>
                            </td>
                        </tr>
                    </tfoot>
                </table>
                <? else: ?>
                    <h4 class="text-success"><i class="icon-meh"></i> &nbsp;很抱歉，没有找到符合条件的记录。</hr>
                <? endif ?>
            </div>
        </div>
    </div>
<? endif ?>
<script type="text/javascript">
    $(document).ready(function(){
        $('.datepicker').datepicker();
    });

    $.Goods = {
        add: function()
        {
            var opt = {width: 1000,height:800, lock: true, opacity: 0.75, title: '<h3>新增 <span class="semi-bold">商品</span></h3>',  close: function(){$.T.refresh()}}; 
            $.dialog.open($.G.domain+'goods/add', opt, false)
        },

        // 批量操作
        batch_act: function(act)
        {
            var sns = $('.goods-sn:checked');
            if( sns.length <= 0)
            {
                $.T.alert("批量操作失败：请先选择商品范围。");
                return false;
            }

            var url   = "";
            var title = "";
            switch(act)
            {
                case 'batch_change_status':
                        title = '<h3>批量编辑 <span class="semi-bold">状态</span></h3>';
                        url   = $.G.domain+'goods/batch_change_status/?'+$.param(sns)
                    break;

                case 'batch_edit_brand':
                        title = '<h3>批量编辑 <span class="semi-bold">品牌</span></h3>';
                        url   = $.G.domain+'goods/batch_change_brand/?'+$.param(sns)
                    break;

                default:
                        $.T.alert("批量操作失败：未知的操作选项。");
                        return false;
                    break;
            }

            var opt = {width: 450,height:400, lock: true, opacity: 0.75, title: title,  close: function(){$.T.refresh()}}; 
            $.dialog.open(url, opt, false)
        },

        // 获取属性值
        get_attr: function(act, parent_id)
        {
            var target;
            var html              = "<option value=\"0\" selected=\"selected\">不限</option>";
            var parent_id         = $.T.to_int(parent_id);
            var act               = $.T.trim(act.toLowerCase());
            var attr_select       = $('select[name="attr_id"]');
            var attr_value_select = $('select[name="attr_value_id"]');
            switch(act)
            {
                case 'attr':
                        target = attr_select;
                        if(parent_id === 0)
                        {
                            attr_select
                                .html(html)
                                .select2('val', attr_select.find('option:first').val());
                            attr_value_select
                                .html(html)
                                .select2('val', attr_value_select.find('option:first').val());
                            return;
                        }
                    break;

                case 'attr_value':
                        target = attr_value_select;
                         if(parent_id === 0)
                        {
                            attr_value_select
                                .html(html)
                                .select2('val', attr_value_select.find('option:first').val());
                            return;
                        }
                    break;

                default:
                        $.T.alert("未知的操作。");
                        return;
                    break;
            }

            $.getJSON($.G.domain+"goods/get_attr/?act="+act+"&is_ajax=1&parent_id="+parent_id, function(response)
            {
                if( false == response.status)
                {
                    $.T.alert(response.msg);
                }
                else
                {
                    $.each(response.data, function(i,n)
                    {
                        html += "<option value=\""+n.id+"\" selected=\"selected\">"+n.name+"</option>";
                    });
                    target.html(html);
                }
            });
            return;
        },

        /**
         * [改变状态]
         */
        change_status: function(sn, act)
        {
            if( $.T.empty(sn) )
            {
                $.T.alert("修改商品状态失败：没有提供商品编码。");
                return false;
            }
            if( $.T.empty(act) )
            {
                $.T.alert("修改商品状态失败：没有提供操作类型。");
                return false;
            }
            $.T.confirm('确认要执行这个操作么？', function(){
                $.getJSON($.G.domain+"goods/change_status/"+sn+"/?act="+act+"&is_ajax=1", function(response)
                {
                    if( false == response.status)
                    {
                        $.T.alert(response.msg);
                    }
                    else
                    {
                        $.T.refresh();
                    }
                });
            });
        },

        edit: function(sn)
        {
            if( $.T.empty(sn) )
            {
                $.T.alert("编辑商品失败：没有提供商品编码。");
                return false;
            }
            var opt = {width: 1000,height:800, lock: true, opacity: 0.75, title: '<h3>编辑 <span class="semi-bold">商品</span></h3>',  close: function(){$.T.refresh()}}; 
            $.dialog.open($.G.domain+'goods/edit/'+sn, opt, false)
        },
    }
</script>